<?php
/**
 * Created by PhpStorm.
 * User: windon
 * Date: 2018/6/3
 * Time: 14:53
 */

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Friend extends Model
{
    protected $table = 'friend_user';

    public function media()
    {
        return $this->belongsTo('App\Models\Media', 'friend_id', 'uid');
    }

    /**
     * 获得推荐的好友列表
     * @param $param  参数集合
     * @param $where array 查询条件
     * @return mixed
     */
/*    public function getFriendListOfRecommending($param=[], $where=[])
    {
        $uid = $param['uid'];
        $page = $param['page'];
        $limit = $param['limit'];
        $where_not_uid = $where;
        unset($where_not_uid[2]);//剔除uid的条件
        $data = DB::table('friend_user as fu')
            ->select('u.id','u.mobile','u.avatar')
            ->join('users as u','fu.friend_id','=','u.id')
            ->where(function ($query) use($where, $uid){
                $query->whereIn('fu.uid', function($query1) use($where){
                    $query1->select('friend_id')
                        ->from('friend_user as fu')
                        ->where($where)->get();
                })->orWhere('fu.uid', '=', $uid)->get();
            })
            ->where($where_not_uid)
            ->orderby('fu.created_at', 'DESC')
            ->offset($page)
            ->limit($limit)
            ->get();
        return $data;
    }*/

    /**
     * 获得关注我的好友列表
     * @param $param  参数集合
     * @param $where array 查询条件
     * @return mixed
     */
   /* public function getFriendListOfStared($param=[], $where=[], $type=[])
    {
        $page = $param['page'];
        $limit = $param['limit'];
        $data = DB::table('friend_user as fu')
            ->select('u.id','u.mobile','u.avatar')
            ->join('users as u','fu.friend_id','=','u.id')
            ->where($where)
            ->whereIn('fu.type', $type)
            ->orderby('fu.created_at', 'DESC')
            ->offset($page)
            ->limit($limit)
            ->get();
        return $data;
    }*/

   /*
    * 改变关注状态
    * @$uid 用户id
    * @$friend_uid 被关注的用户id
    * @status : 1 加入黑名单  2 解除黑名单
    * */
   public static function change_status($user_info, $friend_uid, $status)
   {
       $model = new Friend();
       $sql = "select * from ".$model->getTable()." where ((uid={$user_info->id} and friend_id={$friend_uid}) or (uid={$friend_uid} and friend_id={$user_info->id})) and type=3";
       $list = DB::select($sql);
       if(!empty($list)){

           // 获取用户的粉丝和关注数
           $userList = User::select(['id','attention', 'fans'])->whereIn('id', [$user_info->id, $friend_uid])->get();
           $temp_arr = [];
           foreach ($userList as $k=>$v)
               $temp_arr[$v->id] = $v;

           foreach ($list as $value){
               // 加入黑名单    $value->status：0 无效  1 正常
               if($status == 1 && $value->status == 1){
                    $channel_friend = self::where('id', $value->id)->update(['status' => 0]);
                    if($channel_friend){
                       if($temp_arr[$value->uid]->attention > 0)
                           DB::table('users')->where('id', $value->uid)->decrement('attention');  // 用户关注数-1
                       if($temp_arr[$value->friend_id]->fans > 0)
                           $update =DB::table('users')->where('id', $value->friend_id)->decrement('fans'); // 被取消关注用户粉丝数-1
                    }
                    if(empty($channel_friend))
                       return false;
               }
           }
       }
       return true;
   }

}